var express = require("express");
var router = express.Router();

// 引入封装的 mysql模块
const mysql = require("../util/mysql/mysql");
//互动通知
router.post("/getInteractinf", function (req, res, next) {
  const [w_to, to_type] = [req.body.user_id, req.body.user_type];
  mysql.data(
    "SELECT * FROM leave_words WHERE w_type=3 AND w_to=? AND to_type=?",
    [w_to, to_type],
    function (err, rows, fields) {
      if (err) return;
      res.send(
        JSON.stringify({
          code: 1,
          InteractInfList: rows,
        })
      );
    }
  );
});
//回复消息
router.post("/getReplyInf", function (req, res, next) {
  const [user_id, user_type] = [req.body.user_id, req.body.user_type];
  const replyList = [];
  mysql.data(
    "SELECT * FROM leave_words WHERE w_type=2",
    [],
    function (err, rows1, fields) {
      if (err) return;
      mysql.data(
        "SELECT * FROM leave_words WHERE w_type=1 AND u_id=? AND u_type=?",
        [user_id, user_type],
        function (err, rows2, fields) {
          if (err) return;
          rows1.forEach((e1, i1) => {
            rows2.forEach((e2, i2) => {
              if (e1.w_to == e2.w_id) {
                var reply = { ...rows1[i1], w_toMain: rows2[i2].w_main };
                replyList.push(reply);
              }
            });
          });
          res.send(JSON.stringify({ code: 1, replyList }));
        }
      );
    }
  );
});
//审批消息
router.post("/getApplyInf", function (req, res, next) {
  const [user_id, user_type] = [req.body.user_id, req.body.user_type];
  if (user_type == 1) {
    mysql.data(
      "SELECT al_id,al_main,al_time,e_name,p_title FROM apply_mes a INNER JOIN expert_mes b INNER JOIN project c ON a.al_expert=b.e_id AND a.al_project=c.id WHERE al_student=? AND al_type=1",
      [user_id],
      function (err, rows, fields) {
        if (err) return;
        res.send(
          JSON.stringify({
            code: 1,
            ApplyInfList: rows,
          })
        );
      }
    );
  } else if (user_type == 2) {
    mysql.data(
      "SELECT al_id,al_main,al_time,e_name,p_title FROM apply_mes a INNER JOIN expert_mes b INNER JOIN project c ON a.al_expert=b.e_id AND a.al_project=c.id WHERE al_teacher=? AND al_type=1",
      [user_id],
      function (err, rows, fields) {
        if (err) return;
        res.send(
          JSON.stringify({
            code: 1,
            ApplyInfList: rows,
          })
        );
      }
    );
  } else {
    mysql.data(
      "SELECT al_id,al_main,al_time,s_name,p_title FROM apply_mes a INNER JOIN student_mes b INNER JOIN project c ON a.al_student=b.s_id AND a.al_project=c.id WHERE al_expert=? AND al_type=2",
      [user_id],
      function (err, rows, fields) {
        if (err) return;
        res.send(
          JSON.stringify({
            code: 1,
            ApplyInfList: rows,
          })
        );
      }
    );
  }
});
//系统消息
router.post("/getSystemInf", function (req, res, next) {
  mysql.data("SELECT * FROM system_mes", [], function (err, rows, fields) {
    if (err) return;
    res.send(
      JSON.stringify({
        code: 1,
        SystemInfList: rows,
      })
    );
  });
});
//老师通知
router.post("/getTeacherInf", function (req, res, next) {
  mysql.data(
    "SELECT * FROM leave_words WHERE w_type=4 AND w_to=?",
    [req.body.user_id],
    function (err, rows, fields) {
      if (err) return;
      res.send(
        JSON.stringify({
          code: 1,
          teacherInfList: rows,
        })
      );
    }
  );
});
//学生通知
router.post("/getStudentInf", function (req, res, next) {
  mysql.data(
    "SELECT * FROM leave_words WHERE w_type=5 AND w_to=?",
    [req.body.user_id],
    function (err, rows, fields) {
      if (err) return;
      res.send(
        JSON.stringify({
          code: 1,
          studentInfList: rows,
        })
      );
    }
  );
});
//队伍消息
router.post("/getTeamInf", function (req, res, next) {
  const s_id = req.body.s_id;
  mysql.data(
    "SELECT * FROM team_member a INNER JOIN team b ON a.team_Id=b.team_id WHERE student_id=? AND end=0",
    [s_id],
    function (err, rows1, fields) {
      if (err) return;
      if (rows1.length == 0) {
        res.send(
          JSON.stringify({
            code: 2,
          })
        );
      } else {
        const t_id = rows1[0].team_Id;
        mysql.data(
          "SELECT * FROM team_mes WHERE tl_team=?",
          [t_id],
          function (err, rows2, fields) {
            if (err) return;
            res.send(
              JSON.stringify({
                code: 1,
                teamInfList: rows2,
              })
            );
          }
        );
      }
    }
  );
});
//增加消息
router.post("/addMes", function (req, res, next) {
  const [user_id, user_type] = [req.body.user_id, req.body.user_type];
  if (user_type == 1) {
    mysql.data(
      "SELECT my_mes FROM student_mes WHERE s_id=?",
      [user_id],
      function (err, rows1, fields) {
        if (err) return;
        var mes = rows1[0].my_mes + 1;
        mysql.data(
          "UPDATE student_mes SET my_mes=? WHERE s_id=?",
          [mes, user_id],
          function (err, rows, fields) {
            if (err) return;
            res.send(
              JSON.stringify({
                code: 1,
              })
            );
          }
        );
      }
    );
  } else if (user_type == 2) {
    mysql.data(
      "SELECT my_mes FROM teacher_mes WHERE t_id=?",
      [user_id],
      function (err, rows1, fields) {
        if (err) return;
        var mes = rows1[0].my_mes + 1;
        mysql.data(
          "UPDATE teacher_mes SET my_mes=? WHERE t_id=?",
          [mes, user_id],
          function (err, rows, fields) {
            if (err) return;
            res.send(
              JSON.stringify({
                code: 1,
              })
            );
          }
        );
      }
    );
  }
});
//检测消息变换
router.post("/lookMes", function (req, res, next) {
  var mes = req.body.mes;
  var type = req.body.type;
  if (type == 1) {
    mysql.data(
      "SELECT my_mes FROM student_mes WHERE s_id=?",
      [1],
      function (err, rows1, fields) {
        if (err) return;
        if (mes == rows1[0].my_mes) {
          res.send(
            JSON.stringify({
              code: 2,
            })
          );
        } else {
          res.send(
            JSON.stringify({
              code: 1,
              mes: rows1[0].my_mes,
            })
          );
        }
      }
    );
  } else if (type == 2) {
    mysql.data(
      "SELECT my_mes FROM teacher_mes WHERE t_id=?",
      [1],
      function (err, rows1, fields) {
        if (err) return;
        if (mes == rows1[0].my_mes) {
          res.send(
            JSON.stringify({
              code: 2,
            })
          );
        } else {
          res.send(
            JSON.stringify({
              code: 1,
              mes: rows1[0].my_mes,
            })
          );
        }
      }
    );
  }
});
module.exports = router;
